﻿using God.ShoppingWebApi.Entities;
using God.ShoppingWebApi.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Application.Services;
using Microsoft.AspNetCore.Mvc;
using God.ShoppingWebApi.DTO.Comm;
using Volo.Abp.Application.Dtos;
using Microsoft.EntityFrameworkCore;

namespace God.ShoppingWebApi.Service
{
    public class AdminCommService : ApplicationService, IAdminCommService
    {
        IRepository<CommodInfo, Guid> _repository;
        public AdminCommService(IRepository<CommodInfo, Guid> repository)
        {
            _repository = repository;
        }

     
        /// <summary>
        /// 商品添加
        /// </summary>
        /// <param name="commodInfo"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        /// 
       [HttpPost,Route("app/Gadd")]
        public async Task AddAsync(CommDTO commodInfo)
        {
            CommodInfo ci = new CommodInfo(GuidGenerator.Create(), commodInfo.CommodityName, commodInfo.CommoditySalePrice, commodInfo.CommodityDetails, commodInfo.CommodityImg, commodInfo.CommentCount, commodInfo.CommoditySaleNum, commodInfo.CommodityBorwseNum, CurrentUser.Id);

            await _repository.InsertAsync(ci);
        }
        [Route("api/Gshow")]


        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        ///public async Task<List<CommDTO>> GetListAsync()
        ///{
        ///    var str =await _repository.GetListAsync();
        ///    return ObjectMapper.Map<List<CommodInfo>, List<CommDTO>>(str);  
        ///}
        ///


        [Route("api/Gdel")]
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="gu"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task DeleteAsync(Guid gu)
        {
            await _repository.DeleteAsync(gu);
        }
        [Route("api/Gfan")]
        /// <summary>
        /// 返填
        /// </summary>
        /// <param name="gid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<CommDTO> Edit(Guid gid)
        {
            var data = await _repository.GetAsync(gid);
            var CommDTO = ObjectMapper.Map<CommodInfo, CommDTO>(data);
            return CommDTO;
        }
        [Route("api/Gupdate")]
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="infoDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<CommDTO> Update(CommDTO infoDTO)
        {
            var data = await _repository.GetAsync(infoDTO.Id);
            if (data != null)
            {
                data.LastModificationTime = DateTime.Now;
                data.CommodityName = infoDTO.CommodityName;
                data.CommodityDetails = infoDTO.CommodityDetails;
                data.CommodityImg = infoDTO.CommodityImg;
                data.CommentCount = infoDTO.CommentCount;
                data.CommodityBorwseNum = infoDTO.CommodityBorwseNum;
                data.CommodityBorwseNum = infoDTO.CommodityBorwseNum;
                var Info = await _repository.UpdateAsync(data);

                return infoDTO;
            }
            else
            {
                return null;
            }

            //var data = ObjectMapper.Map<CommDTO, CommodInfo>(infoDTO);


        }
        /// <summary>
        /// 分页显示排序
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PagedResultDto<CommDTO>> GetListAsync(PagedAndSortedResultRequestDto input)
        {
            var query = await _repository.GetQueryableAsync();

            query = query.OrderBy(input.Sorting);

            var totalCount = await query.CountAsync();

            List<CommodInfo> commodInfos = await query
                .Skip(input.SkipCount)
                .Take(input.MaxResultCount)
                .ToListAsync();

            List<CommDTO> commods = ObjectMapper.Map<List<CommodInfo>, List<CommDTO>>(commodInfos);

            return new PagedResultDto<CommDTO>(totalCount, commods);
        }
    }
}
